package com.maket.jbxx.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.CodeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.maket.jbxx.domain.JldwMeasurementUnit;
import com.maket.jbxx.service.IJldwMeasurementUnitService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 计量单位Controller
 * 
 * @author ruoyi
 * @date 2024-06-24
 */
@RestController
@RequestMapping("/unit")
public class JldwMeasurementUnitController extends BaseController
{
    @Autowired
    private IJldwMeasurementUnitService jldwMeasurementUnitService;

    @Resource
    private CodeClient codeClient;

    /**
     * 查询计量单位列表
     */
    @RequiresPermissions("jbxx:unit:list")
    @GetMapping("/list")
    public TableDataInfo list(JldwMeasurementUnit jldwMeasurementUnit)
    {
        startPage();
        List<JldwMeasurementUnit> list = jldwMeasurementUnitService.selectJldwMeasurementUnitList(jldwMeasurementUnit);
        return getDataTable(list);
    }

    /**
     * 导出计量单位列表
     */
    @RequiresPermissions("jbxx:unit:export")
    @Log(title = "计量单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JldwMeasurementUnit jldwMeasurementUnit)
    {
        List<JldwMeasurementUnit> list = jldwMeasurementUnitService.selectJldwMeasurementUnitList(jldwMeasurementUnit);
        ExcelUtil<JldwMeasurementUnit> util = new ExcelUtil<JldwMeasurementUnit>(JldwMeasurementUnit.class);
        util.exportExcel(response, list, "计量单位数据");
    }

    /**
     * 获取计量单位详细信息
     */
    @RequiresPermissions("jbxx:unit:query")
    @GetMapping(value = "/{dwCode}")
    public AjaxResult getInfo(@PathVariable("dwCode") String dwCode)
    {
        return success(jldwMeasurementUnitService.selectJldwMeasurementUnitByDwCode(dwCode));
    }

    /**
     * 新增计量单位
     */
    @RequiresPermissions("jbxx:unit:add")
    @Log(title = "计量单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JldwMeasurementUnit jldwMeasurementUnit)
    {
        jldwMeasurementUnit.setDwCreate(SecurityUtils.getUsername());
        jldwMeasurementUnit.setDwCode(codeClient.getCode(22));
        jldwMeasurementUnit.setChangeMany(jldwMeasurementUnit.getChangeMany()+":"+jldwMeasurementUnit.getChangeManyTwo());


        return toAjax(jldwMeasurementUnitService.insertJldwMeasurementUnit(jldwMeasurementUnit));
    }

    /**
     * 修改计量单位
     */
    @RequiresPermissions("jbxx:unit:edit")
    @Log(title = "计量单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JldwMeasurementUnit jldwMeasurementUnit)
    {
        jldwMeasurementUnit.setDwCreate(SecurityUtils.getUsername());
        jldwMeasurementUnit.setChangeMany(jldwMeasurementUnit.getChangeMany()+":"+jldwMeasurementUnit.getChangeManyTwo());

        return toAjax(jldwMeasurementUnitService.updateJldwMeasurementUnit(jldwMeasurementUnit));
    }

    /**
     * 删除计量单位
     */
    @RequiresPermissions("jbxx:unit:remove")
    @Log(title = "计量单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dwCodes}")
    public AjaxResult remove(@PathVariable String[] dwCodes)
    {
        return toAjax(jldwMeasurementUnitService.deleteJldwMeasurementUnitByDwCodes(dwCodes));
    }
}
